package com.soul.recommend.listener;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.soul.model.db.Log;
import com.soul.model.mongo.Movement;
import com.soul.model.mongo.MovementScore;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author mdoya
 * @version 1.0
 * @description: 圈子推荐-打分业务实现（为了大数据推荐系统提供基础数据）
 */
@Slf4j
@Component
public class RecommendLogListener {

    @Autowired
    private MongoTemplate mongoTemplate;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "soul.log.queue",durable = "true"),
            exchange = @Exchange(value = "soul.log.exchange",type = ExchangeTypes.TOPIC),
            key = {"log.movement"} // 绑定key
    ))
    public void listenerMessage(String message){
        log.debug("开始发布动态打分业务listener监听");
        // 1. 解析字符串
        Map<String,String> map = JSON.parseObject(message, Map.class);
        String userId = map.get("userId");
        String type = map.get("type");
        String logTime = map.get("logTime");
        String busId = map.get("busId");// 业务对象id
        // 2. 查询发布动态id是否存在
        Movement movement = mongoTemplate.findById(new ObjectId(busId), Movement.class);
        if (movement != null){
            // 3. 开始打分
            MovementScore movementScore = new MovementScore();
            movementScore.setUserId(Long.valueOf(userId));
            movementScore.setMovementId(movement.getPid()); // 大数据系统pic
            movementScore.setDate(System.currentTimeMillis());
            movementScore.setScore(getScore(type,movement));
            mongoTemplate.insert(movementScore);
        }
        log.debug("结束发布动态打分业务listener监听");
    }

    /**
     * 打分业务方法
     * @param type 业务类型
     * @param movement 动态对象
     * @return double
     */
    private double getScore(String type, Movement movement){
        //0201为发动态  基础5分 文本内容50以内1分，50~100之间2分，100以上3分，每张图片1分
        //0202为浏览动态， 1
        //0203为动态点赞， 5
        //0204为动态喜欢， 8
        //0205为评论，     10
        //0206为动态取消点赞， -5
        //0207为动态取消喜欢   -8
        Double score = 0d;
        switch (type) {
            case "0201": // 发布动态, 每多一张图片+1
                score = 5d;
                score += movement.getMedias().size();//图片
                // 文本的长度
                int length = StrUtil.length(movement.getTextContent());
                if (length >= 0 && length < 50) {
                    score += 1;
                } else if (length < 100) {
                    score += 2;
                } else {
                    score += 3;
                }
                break;
            case "0202":
                score = 1d;
                break;
            case "0203":
                score = 5d;
                break;
            case "0204":
                score = 8d;
                break;
            case "0205":
                score = 10d;
                break;
            case "0206":
                score = -5d;
                break;
            case "0207":
                score = -8d;
                break;
            default:
                break;
        }
        return score;
    }
}
